package com.jane.gzzb;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jane.adapter.categoryadapter.ZBInnerCategoryFragmentAdapter;
import com.jane.fragment.category.VideoCategoryFragment;
import com.jane.fragment.category.ZBCategoryFragment;

import java.util.ArrayList;

public class InnerCategoryActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager vp;
    private RadioGroup rg;
    private ArrayList<RadioButton> radioButtons;
    private ArrayList<Fragment> fragments;
    private String gameName;
    private ZBInnerCategoryFragmentAdapter adapter;
    private ImageView iv_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_category);
        //获取上一个页面传递的值
        Intent intent = getIntent();
        if(intent!=null){
            gameName=intent.getStringExtra("gameName");
        }

        initView();
        initData();
        setListener();
    }

    private void initView() {
        iv_back= (ImageView) findViewById(R.id.iv_return_inner_category);
        vp= (ViewPager) findViewById(R.id.vp_inner_category);
        rg= (RadioGroup) findViewById(R.id.rg_inner_category);
    }

    private void initData() {
        fragments=new ArrayList<>();
        radioButtons=new ArrayList<>();
        for (int i = 0; i <rg.getChildCount(); i++) {
            radioButtons.add((RadioButton) rg.getChildAt(i));
        }
        radioButtons.get(0).setChecked(true);
        radioButtons.get(0).setTextColor(Color.parseColor("#FF6F4D"));
        radioButtons.get(1).setTextColor(Color.parseColor("#ffffff"));

        //创建对应的碎片并添加(直播)
        ZBCategoryFragment zbCategoryFragment=new ZBCategoryFragment();
        if(gameName!=null){
            Bundle bundle=new Bundle();
            bundle.putString("gameName",gameName);
            zbCategoryFragment.setArguments(bundle);
        }
        fragments.add(zbCategoryFragment);
        //(视频)
        VideoCategoryFragment videoCategoryFragment=new VideoCategoryFragment();
        fragments.add(videoCategoryFragment);

        //创建适配器
        adapter=new ZBInnerCategoryFragmentAdapter(getSupportFragmentManager(),fragments);
        vp.setAdapter(adapter);
    }

    private void setListener() {
        rg.setOnCheckedChangeListener(this);
        vp.addOnPageChangeListener(this);
        iv_back.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i <radioButtons.size(); i++) {
            if(radioButtons.get(i).getId()==checkedId){
                radioButtons.get(i).setTextColor(Color.parseColor("#FF6F4D"));
                //跳转到对应的ViewPager页面
                vp.setCurrentItem(i);
            }else{
                radioButtons.get(i).setTextColor(Color.parseColor("#ffffff"));
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //根据Viewpager的滑动选中不同的RadioButton
        radioButtons.get(position).setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    //设置点击返回注销该页面
    @Override
    public void onClick(View v) {
        finish();
    }
}
